package edu.jxut.reggie.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.jxut.reggie.dtos.SetmealDto;
import edu.jxut.reggie.dtos.SetnealDtooo;
import edu.jxut.reggie.mapper.SetmealMapper;
import edu.jxut.reggie.pojos.Setmeal;
import edu.jxut.reggie.pojos.SetmealDish;
import edu.jxut.reggie.server.SetmealDishService;
import edu.jxut.reggie.server.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealMapper setmealMapper;


    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((setmealDish) -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        List<Setmeal> list = this.list(new LambdaQueryWrapper<Setmeal>().in(Setmeal::getId, ids));
        List<Long> setmealIds = list.stream().map(setmeal -> setmeal.getId()).collect(Collectors.toList());

        //删除setmeal
        this.removeByIds(setmealIds);
        //删除dish
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId,setmealIds));
    }

    @Override
    public Page<SetmealDto> selectByPage(Integer size, Integer pageSize,String name) {
        //查询setmal表
        Page<Setmeal> setmeal = new Page<>();
        setmeal.setPages(size);
        setmeal.setSize(pageSize);

        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();

        Page<Setmeal> setmealPage = setmealMapper.selectPage(setmeal
                ,new LambdaQueryWrapper<Setmeal>()
                        .like(StringUtils.isNotBlank(name)
                                ,Setmeal::getName,name));

        List<Setmeal> records = setmealPage.getRecords();

        //构建dto
        records.stream().forEach((s)->{
            Long id = s.getId();
            List<SetmealDish> list = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getDishId, id));
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(s,setmealDto);
            setmealDto.setSetmealDishes(list);
            setmealDtos.add(setmealDto);
        });

        Page<SetmealDto> setmealDtoPage = new Page<>(size, pageSize);
        setmealDtoPage.setRecords(setmealDtos);
        return setmealDtoPage;
    }

    @Override
    public SetmealDto selectOne(Long setmealId) {
        Setmeal setmeal = this.getById(setmealId);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        List<SetmealDish> list = setmealDishService
                .list(new LambdaQueryWrapper<SetmealDish>()
                        .eq(SetmealDish::getSetmealId, setmealId)
                );

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    @Transactional
    public void UpdateWithDish(SetmealDto setmealDto) {
        setmealMapper.updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((setmealDish) -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        });

        //查询之前的dishId并删除！
        List<SetmealDish> list = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, setmealDto.getId()));

        List<Long> collect = list.stream().map(dish -> dish.getId()).collect(Collectors.toList());

        setmealDishService.removeByIds(collect);

        //保存新的dish
        setmealDishService.saveOrUpdateBatch(setmealDishes);
    }

    @Override
    public void BulkEditStatus(Integer status, List<Long> ids) {
        ArrayList<Setmeal> setmeals = new ArrayList<>();

        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmeals.add(setmeal);
        }
        this.updateBatchById(setmeals);
    }
    public SetnealDtooo selectOneOne(Long setmealId) {
        Setmeal setmeal = this.getById(setmealId);
        SetnealDtooo setmealDto = new SetnealDtooo();
        BeanUtils.copyProperties(setmeal,setmealDto);

        List<SetmealDish> list = setmealDishService
                .list(new LambdaQueryWrapper<SetmealDish>()
                        .eq(SetmealDish::getSetmealId, setmealId)
                );

        setmealDto.setFlavors(list);

        return setmealDto;
    }
}
